<?php
/**
 * User signup form.
 *
 * @category Zend
 * @package Form
 * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc.
 * @version $Id$
 * @link http://framework.zend.com
 * @since Available since release 1.0
 * @license GPL v3
 */

class Application_Form_Signup extends Twitter_Form
{
    protected $_usernameLength = array(
        Zend_Validate_StringLength::TOO_SHORT => 'Username must be at least 4 chars.',
        Zend_Validate_StringLength::TOO_LONG => 'Username cannot be longer than 32 chars.'
    );
    
    protected $_usernameTaken = array(
        Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND => 'Username is already taken.',
    );
    
    protected $_usernameAlnum = array(
        Zend_Validate_Alnum::INVALID => 'Username can only contain letters and numbers.',
        Zend_Validate_Alnum::NOT_ALNUM => 'Username can only contain letters and numbers.',
    );
    
    protected $_passwordInvalid = array(
        Zend_Validate_StringLength::TOO_SHORT => 'Password must be at least 7 chars.',
        Zend_Validate_StringLength::TOO_LONG => 'Password cannot be longer than 32 chars.'
    );
    
    protected $_passwordMismatch = array(
        Zend_Validate_Identical::NOT_SAME => 'Must match Password entered.'
    );
    
    protected $_emailTaken = array(
        Zend_Validate_Db_NoRecordExists::ERROR_RECORD_FOUND => 'Email address is used for an existing account.'
    );
    
    protected $_emailInvalid = array(
        Zend_Validate_EmailAddress::DOT_ATOM => 'Email Address invalid.',
        Zend_Validate_EmailAddress::INVALID => 'Email Address invalid.',
        Zend_Validate_EmailAddress::INVALID_FORMAT => 'Email Address invalid.',
        Zend_Validate_EmailAddress::INVALID_HOSTNAME => 'Email Address invalid.',
        Zend_Validate_EmailAddress::INVALID_LOCAL_PART => 'Email Address invalid.',
        Zend_Validate_EmailAddress::INVALID_MX_RECORD => 'Email Address invalid.',
        Zend_Validate_EmailAddress::INVALID_SEGMENT => 'Email Address invalid.',
        Zend_Validate_EmailAddress::LENGTH_EXCEEDED => 'Email Address invalid.',
        Zend_Validate_EmailAddress::QUOTED_STRING => 'Email Address invalid.'
    );
    
    /**
     * Setup form elements
     */
    public function init()
    {
        $this->setLegend('SIGNUP');
        $this->setAttrib('horizontal', true);
        
        $this->addElement('text', 'username', array(
            'label' => 'Username',
            'description' => 'Choose a Disputeo username.',
            'required' => true,
            'validators' => array(
                array(
                    'validator' => 'Alnum',
                    'options' => array(
                        'messages' => $this->_usernameAlnum    
                    )
                ),
                array(
                    'validator' => 'StringLength',
                    'options' => array(
                        'max' => 32,
                        'min' => 4,
                        'encoding' => 'UTF-8',
                        'messages' => $this->_usernameLength
                    )
                ),
                array(
                    'validator' => 'Db_NoRecordExists',
                    'options' => array(
                        'table' => 'user',
                        'field' => 'userName',
                        'messages' => $this->_usernameTaken
                    )
                )
            )
        ));
        
        $this->addElement('password', 'password', array(
            'label' => 'Password',
            'description' => 'Enter a password for your Disputeo account.',
            'required' => true,
            'renderPassword' => true,
            'validators' => array(
                array(
                    'validator' => 'StringLength',
                    'options' => array(
                        'max' => 32,
                        'min' => 7,
                        'encoding' => 'UTF-8',
                        'messages' => $this->_passwordInvalid
                    )
                )
            )
        ));
        
        $this->addElement('password', 'verify', array(
            'label' => 'Verify',
            'description' => 'Re-enter password.',
            'required' => true,
            'renderPassword' => true,
            'validators' => array(
                array(
                    'validator' => 'Identical',
                    'options' => array(
                        'token' => 'password',
                        'encoding' => 'UTF-8',
                        'messages' => $this->_passwordMismatch
                    )
                )
            )
        ));
        
        $this->addElement('text', 'email', array(
            'label' => 'Email',
            'description' => 'Provide an email address (optional).',
            'validators' => array(
                'EmailAddress',
                array(
                    'validator' => 'EmailAddress',
                    'messages' => $this->_emailInvalid
                ),
                array(
                    'validator' => 'StringLength',
                    'options' => array(
                        'max' => 32,
                        'encoding' => 'UTF-8'
                    )
                ),
                array(
                    'validator' => 'Db_NoRecordExists',
                    'options' => array(
                        'table' => 'userInfo',
                        'field' => 'email',
                        'messages' => $this->_emailTaken
                    )
                )
            )
        ));
        
        $this->addElement(new Disputeo_Form_Element_Captcha('captcha', array(
            'description' => 'Enter the text displayed above.',
            'label' => 'Test',
            'required' => true,
        )));
        
        $this->getElement('captcha')
            ->removeDecorator('ViewHelper');
        
        $this->addElement('checkbox', 'rememberme', array(
            'label' => 'Remember Me',
            'checked' => true
        ));
        
        $this->addElement('submit', 'signup', array(
            'label' => 'Signup',
            'class' => 'btn'
        ));   
    }
}